package cn.yh.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

public class SqlsessionFactoryUtil {
    private static SqlSessionFactory factory;
    private SqlsessionFactoryUtil(){}

    static {
        Reader reader=null;
        try {
            reader= Resources.getResourceAsReader("mybatis.xml");
            factory=new SqlSessionFactoryBuilder().build(reader);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(reader!=null){
                try {
                    reader.close();
                } catch (IOException e) {
                }
            }
        }
    }
    public static SqlSession getSession(){
       return factory.openSession();
    }

}
